# coding:utf-8

from datetime import date

from wtforms import SubmitField, BooleanField
from wtforms.validators import Optional, DataRequired
from wtforms_components import StringField, DateField

from app.forms import WphForm, NonValidatingSelectField, NotChoices
from app.op.models import WxUser, User, UserInfo


class SearchEventStatForm(WphForm):
    """
    搜索用户表单
    """

    event = StringField('事件', validators=[Optional()])
    date = DateField('结束日期', validators=[Optional()])


class DefaultStatSearchForm(WphForm):
    """
    created by chenhj at 2017/9/8

    """
    start_date = DateField('开始日期', validators=[Optional()])
    end_date = DateField('结束日期', validators=[Optional()], default=date.today)


class OperateExportForm(DefaultStatSearchForm):
    """
    created by chenhj at 2018/9/6

    """
    email = StringField('邮箱地址', validators=[DataRequired()])


class SearchWechatStatForm(WphForm):
    """
    搜索微信用户列表表单
    """

    invite_user_id = NonValidatingSelectField('邀请人', validators=[Optional()], coerce=int)
    only_ground = BooleanField('仅显示地推人员', default=False)

    submit = SubmitField('查询')

    def __init__(self, *args, **kwargs):
        super(SearchWechatStatForm, self).__init__(*args, **kwargs)

        user_ids = WxUser.get_all_invite_user_ids()
        users = User.get_list_by_ids(user_ids)

        self.invite_user_id.choices = NotChoices + \
            [
                (user.id, user.nickname)
                for user in users
            ]
        self.invite_user_id.default = NotChoices[0][0]


class SearchFeedbackStatForm(WphForm):
    """
    搜索退单统计表单
    """

    mobile = StringField('手机号', validators=[Optional()])


class SearchAppPromotionStatForm(WphForm):
    """
    搜索app推广统计表单
    """

    mobile = StringField('手机号', validators=[Optional()])
    app_invite_user_id = NonValidatingSelectField('邀请人', validators=[Optional()], coerce=int)
    app_root_invite_user_id = NonValidatingSelectField('根节点', validators=[Optional()], coerce=int)

    submit = SubmitField('查询')

    def __init__(self, *args, **kwargs):
        super(SearchAppPromotionStatForm, self).__init__(*args, **kwargs)

        invite_users = UserInfo.get_all_app_invite_users()
        self.app_invite_user_id.choices = NotChoices + \
            [
                (user.id, user.nickname)
                for user in invite_users
            ]
        self.app_invite_user_id.default = NotChoices[0][0]

        root_invite_users = UserInfo.get_all_app_root_invite_users()
        self.app_root_invite_user_id.choices = NotChoices + \
              [
                  (user.id, user.nickname)
                  for user in root_invite_users
              ]
        self.app_root_invite_user_id.default = NotChoices[0][0]


class SearchAppPromotionCensForm(WphForm):
    """
    搜索app推广统计表单
    """

    mobile = StringField('手机号', validators=[Optional()])
    app_root_invite_user_id = NonValidatingSelectField('根节点', validators=[Optional()], coerce=int)

    submit = SubmitField('查询')

    def __init__(self, *args, **kwargs):
        super(SearchAppPromotionCensForm, self).__init__(*args, **kwargs)

        root_invite_users = UserInfo.get_all_app_root_invite_users()

        self.app_root_invite_user_id.choices = NotChoices + \
              [
                  (user.id, user.nickname)
                  for user in root_invite_users
              ]
        self.app_root_invite_user_id.default = NotChoices[0][0]

